import time

from selenium import webdriver
import pytest

from .Button import Button
from .alert import *


def test_info_click():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/alert/index.html")
    time.sleep(2)
    try:
        btn = Button(browser, "btn1", Selector.ID)
        btn.click()
        time.sleep(1)
        alert = AlertInfo(browser)
        alert.click()
        time.sleep(2)
    finally:
        browser.close()


def test_success_click():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/alert/index.html")
    time.sleep(2)
    try:
        btn = Button(browser, "btn2", Selector.ID)
        btn.click()
        time.sleep(1)
        alert = AlertSuccess(browser)
        alert.click()
        time.sleep(2)
    finally:
        browser.close()


def test_warn_click():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/alert/index.html")
    time.sleep(2)
    try:
        btn = Button(browser, "btn3", Selector.ID)
        btn.click()
        time.sleep(1)
        alert = AlertWarn(browser)
        alert.click()
        time.sleep(2)
    finally:
        browser.close()


def test_error_click():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/alert/index.html")
    time.sleep(2)
    try:
        btn = Button(browser, "btn4", Selector.ID)
        btn.click()
        time.sleep(1)
        alert = AlertError(browser)
        alert.click()
        time.sleep(2)
    finally:
        browser.close()


def test_confirm_click():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/alert/index.html")
    time.sleep(2)
    try:
        btn = Button(browser, "btn5", Selector.ID)
        btn.click()
        time.sleep(1)
        alert = AlertConfirm(browser)
        alert.click()
        time.sleep(2)
    finally:
        browser.close()


def test_blocked_confirm_click():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/alert/index.html")
    time.sleep(2)
    try:
        btn = Button(browser, "btn7", Selector.ID)
        btn.click()
        time.sleep(1)
        alert = AlertConfirm(browser)
        alert.click()
        time.sleep(2)
    finally:
        browser.close()


def test_process_alert():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/alert/index.html")
    time.sleep(2)
    try:
        btn = Button(browser, "btn6", Selector.ID)
        btn.click()
        time.sleep(1)
        process_fish_modal_dialog_alert(browser, button='OK', prompt_text="mytest")
        time.sleep(2)
    finally:
        browser.close()


def test_process_alert_info():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/alert/index.html")
    time.sleep(2)
    try:
        btn = Button(browser, "btn1", Selector.ID)
        btn.click()
        time.sleep(1)
        process_fish_modal_dialog_alert(browser, button='OK')
        time.sleep(2)
    finally:
        browser.close()


def test_process_alert_info_with_prompt_text_error():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/alert/index.html")
    time.sleep(2)
    try:
        btn = Button(browser, "btn1", Selector.ID)
        btn.click()
        time.sleep(1)
        with pytest.raises(TestException):
            process_fish_modal_dialog_alert(browser, button='OK', prompt_text="mytest")
        time.sleep(2)
    finally:
        browser.close()


if __name__ == '__main__':
    pytest.main(['-s', 'alert_test.py'])
